$(function(){
	$('.list-content-div table thead tr th:last').attr("width","8%");
	//+号点击事件
	$(".addTableModalbtn").click(function() {
		$('#addTableModal').modal({show:true,moveable:true,position:0});
		$('#adddaynumber').val("0"); 
		$('.showtext').val(""); 
	});
	//非数字过滤
	$('#add_distance').on('keyup',function(){
		var number=$(this).val();
		number=number.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
		number=number.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的  
		number=number.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		number=number.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数  
		if(number.indexOf(".")< 0 && number!=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
			number= parseFloat(number);
		}
		$(this).val(number);
	});
	$('#edit_distance').on('keyup',function(){
		var number=$(this).val();
		number=number.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
		number=number.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的  
		number=number.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		number=number.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数  
		if(number.indexOf(".")< 0 && number!=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
			number= parseFloat(number);
		}
		$(this).val(number);
	});
	$('#adddaynumber').on('keyup',function(){
		//验证正整数
		var number=$(this).val();
		number=number.replace(/[^\d]/g,"");  //清除“数字”和“.”以外的字符  
		number=number.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		number=number.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数  
		if(number.indexOf(".")< 0 && number!=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
			number= parseFloat(number);
		}
		$(this).val(number);
	});
	$('#editdaynumber').on('keyup',function(){
		//验证正整数
		var number=$(this).val();
		number=number.replace(/[^\d]/g,"");  //清除“数字”和“.”以外的字符  
		number=number.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		number=number.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数  
		if(number.indexOf(".")< 0 && number!=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
			number= parseFloat(number);
		}
		$(this).val(number);
	});

	//F4tree点击确定
	$(".treeView .btn-success").on('click',function(){
    var treeView= $('#edit_destination').f4tree().getValue();  
    $("#addchoosePosition").val(treeView[0].name);
    $("#add_position_code").val(treeView[0].id);
	});
    $('.treeView').delegate('.node_name', 'dblclick', function() {
    var treeView= $('#edit_destination').f4tree().getValue();  
	$("#addchoosePosition").val(treeView[0].name);
	$("#add_position_code").val(treeView[0].id);
	});
    
    //关闭F4tree
    $('#addTableModal').on('hidden.zui.modal', function() {
    	$(".treeView .btn-cancel").click();
    });
    
});
	//新增
	//获得新增计划模板窗口
	$(".addPlanTemplate").on('click',function(){
	   	 $("#addDetermine").show();
		 $("#editDetermine").hide();
		 $(".Tbody").html('');
		 $("#add_position_code").val("");
		 $("#addremarks").val("");
		 $.get($('body').attr('ctxPath')+'/planTemplate/getTransportMode?t='+new Date().getTime(),function(data){
             if(/*data.state 验证待用*/ false){
            	 layer.open({
		 				  title: '消息提示',
		 				  offset: '200px',
		 				  content:"请先补充运输方式信息!"
		 			       });
            	 return;
             }else{
            		//根据company-id获取计划模板信息
         		$.get($('body').attr('ctxPath')+'/planTemplate/getAddPlanTemplate?t='+new Date().getTime(),function(data){
         				if(data.state){
         					$('#positionCode').val("");
         					$('#plan_template_name').val("");
         					$('.addRemarks').val("");
         					if(!data.generateCoding.flag){
         						layer.alert(data.generateCoding.msg);
         						return;
         					}else{
         						$("#plan_template_code").val(data.generateCoding.msg);
         					}
         					var transportModes=data.transportModes//transportModes对象
         					var html1 = ['<option value="-1">---请选择运输方式---</option>'];
         					for(var i = 0; i < transportModes.length; i++){
         						html1.push('<option value="'+transportModes[i].id+'">'+transportModes[i].transportChineseName+'</option>');
         					}
         					var a=document.getElementById('transport');
         					a.options.length=0;
         					$('.transportChineseName').append(html1.join(''));
         					$(".comm").parent().html('');
         					$("#add_start_place").val('');
         					$("#add_distination").val("");
         					$("#add_position_code").val('');
         					$('#addWindow').modal({
         				 		moveable:true,
         				 		position: 30,
         				 		show: true
         				 	});
         			 	}else{
         					layer.open({
         		 				  title: '消息提示',
         		 				  offset: '200px',
         		 				  content:"获取数据失败!"
         		 			});
         		 			$('body').scrollTop(100);
         				}
         		});
             }
             });
	});
	//新增确认位置与天数
	$("#addDetermine").on('click',function(){
		var tag=true;
		var positioncode= $("#add_position_code").val();
		 $.get($('body').attr('ctxPath')+'/planTemplate/findPositionByCode?t='+new Date().getTime()+"&positionCode="+positioncode,function(data){
				var btRegPosition=data.btRegPosition;
		if(btRegPosition==null&&data.regPositionCode!=''){
			tag=false;
			$("#add_position_code").val('');
			layer.msg("请选择一个位置,不要选择区域!");
			return;
		   }
		 if(tag){
				var PositionCode= $("#add_position_code").val();
				var PositionName=$("#addchoosePosition").val();
				var daynumber=$(".daynumber").val();
				var sign=new Number; 
				sign=$("#addtbody>tr:last-child>td:nth-child(1)").html();
				if (sign==undefined) {
					sign=1;
				}else{
					sign=parseInt(sign)+1;
				}
				var tablehtml=("<tr><td>"+sign+"</td> <td>"+PositionName+"</td> <td>"+daynumber+"</td><td><a href='javascript:;' class='deleteJourlize'>删除</a></td><td hidden='hidden'>"+PositionCode+"</td></tr> ")
			    $("#addtbody").append(tablehtml);
				var tablenum=$("#addtbody").find("tr");
				if(tablenum.length==0){
					$(".startplace").val("");
					$(".distination").val("");
				}
				if(tablenum.length==1){
					$(".startplace").val($(".Tbody>tr:first-child>td:nth-child(2)").html());
					$(".distination").val("");
				}else if(tablenum.length>1){
					$(".startplace").val($(".Tbody>tr:first-child>td:nth-child(2)").html());
					$(".distination").val($(".Tbody>tr:last-child>td:nth-child(2)").html());
				}
				$('#addTableModal').modal('hide');
				 }
				});
			//删除新增途经点
	     $('table>tbody').delegate('tr>td>.deleteJourlize', 'click', function() {
	    	 $(this).parent().parent().remove();
	    		var tablenum=$("#addtbody").find("tr");
				if(tablenum.length==0){
					$(".startplace").val("");
					$(".distination").val("");
				}
				if(tablenum.length==1){
					$("#add_start_place").val($("#addtbody>tr:first-child>td:nth-child(2)").html());
					$("#add_distination").val("");
				}else if(tablenum.length>1){
					$("#add_start_place").val($("#addtbody>tr:first-child>td:nth-child(2)").html());
					$("#add_distination").val($("#addtbody>tr:last-child>td:nth-child(2)").html());
				}
				var sign=0;
	    		$("#addtbody").find("tr").each(function(){ 
	    				$(this).children('td:eq(0)').text(sign+1);
	    				 sign=sign+1;
	    		});
	          });
         });
	//保存 新增计划模板窗口的 信息
    $("#add").on('click',function(){
   	var tag=true;
   	var startplace= $("#add_start_place").val();
   	var distination= $("#add_distination").val();
   	var addremarks= $("#addremarks").val();
   	var plantemplatecode= $("#plan_template_code").val();
   	var plantemplatename= $("#plan_template_name").val();
   	var transport= $("#transport").val();
   	var distance= $("#add_distance").val();
   	if(plantemplatename==""){
   		tag=false;
   		layer.msg("请填写模版名称");
   		return;
   	}
   	if(addremarks==""){
   		tag=false;
   		layer.msg("请填写模版描述");
   		return;
   	}
   	if(transport==-1){
   		layer.msg("请选择运输方式");
   		tag=false;
   		return;
   	}
	
   	if(tag){
   		 //table sign装箱
	    	 var signArr = new Array();
			 var signStr;
			  $("#addtbody").find("tr").each(function () {
				  signArr.push($(this).children('td:eq(0)').text().trim());
				  signStr=signArr.join(";");
			  });
			 //table position装箱
		  	 var positionArr = new Array();
			 var positionStr;
			  $("#addtbody").find("tr").each(function () {
				  positionArr.push($(this).children('td:eq(1)').text().trim());
				  positionStr=positionArr.join(";");
				  });
		    //table day装箱
	    	 var dayArr = new Array();
			 var dayStr;
			  $("#addtbody").find("tr").each(function () {
				  dayArr.push($(this).children('td:eq(2)').text().trim());
				  dayStr=dayArr.join(";");
			  });
			  //table positionCode装箱
	    	 var positionCodeArr = new Array();
			 var positionCodeStr;
			  $("#addtbody").find("tr").each(function () {
				  positionCodeArr.push($(this).children('td:eq(4)').text().trim());
				  positionCodeStr=positionCodeArr.join(";");
			  });	  
			  $.post($('body').attr('ctxPath') +'/planTemplate/addPlanTemplate?t='+new Date().getTime(),
			      {"startplace":startplace,"distination":distination,"addremarks":addremarks,"plantemplatecode":plantemplatecode,"plantemplatename":plantemplatename,
				  "transport":transport, "distance":distance,"signStr":signStr,"positionStr":positionStr,"dayStr":dayStr,"positionCodeStr":positionCodeStr},function(data){
					  layer.open({
						  title: '消息提示',
						  offset: '200px',
						  content: data.msg
					  });
					  $('#addWindow').modal('hide');
					  $('body').scrollTop(100);
					  listui.refresh('mainUI');
			  });
      	}
  });
//编辑
//获得编辑页面
$('.list-content-div table>tbody').delegate('tr>td>.edits', 'click', function() {
	 $("#addDetermine").hide();
	 $("#editDetermine").show();
	 $("#edit_position_code").val("");
	 $(".Tbody").html('');
     var id = $(this).attr('data-id');
	//根据id获取计划模板信息
	$.get($('body').attr('ctxPath')+'/planTemplate/getEditPlanTemplate/'+id+'?t='+new Date().getTime(),function(data){
			if(data.state){
				var planTemplate=data.obj//obj对象
				var positionSet=data.positionSet;//位置集合
				$("#edit_positionSet").empty().val(positionSet);
				$("#edit_start_place").val("");
				$("#edit_distination").val("");
				$("#edit_destination").prev().prev().val("");
				$("#editid").val(planTemplate.id);
				$("#edit_start_place_id").val(planTemplate.startPlaceId);
				$("#edit_destination_id").val(planTemplate.destinationId);
				$("#editplanTemplateCode").val(planTemplate.planTemplateCode);
				$("#editplanTemplateName").val(planTemplate.planTemplateName);
				$('#edit_distance').val(planTemplate.distance);
				$("#editremarks").val(planTemplate.remarks);
				var transportModes=data.transportModes//transportModes对象
				var planTemplates =data.planTemplates//planTemplates对象
				var planTemplateList=data.planTemplateList//planTemplateList对象
				var btPositions=data.btPositions//btPositions对象
				var html1 = [];
				var html=[];
				var transportId=planTemplate.id;
				var startPlaceCode=planTemplate.startPlaceCode;
				var destinationCode=planTemplate.destinationCode;
				for(var i = 0; i < transportModes.length; i++){
					var select = '';
					if(transportId == transportModes[i].id){
						select = 'selected="selected"';
					}
					html1.push('<option '+select+' value="'+transportModes[i].id+'">'+transportModes[i].transportChineseName+'</option>');
				}
				for(var i = 0; i < btPositions.length; i++){
					if(startPlaceCode==btPositions[i].PositionCode){
						$("#edit_start_place").val(btPositions[i].PositionName);
					}
				}
				for(var i = 0; i < btPositions.length; i++){
					if(destinationCode==btPositions[i].PositionCode){
						$("#edit_distination").val(btPositions[i].PositionName);
					}
				}
				var a=document.getElementById('edittransport');
				a.options.length=0;
				$('.edittransport').append(html1.join(''));
				for(var i = 0; i <planTemplateList.length; i++){
					html=("<tr><td>"+planTemplateList[i].sign+"</td> <td>"+planTemplateList[i].positionName+"</td> <td>"+planTemplateList[i].days+"</td>" +
							"<td><a href='javascript:;' class='editdeleteJourlize'>删除</a></td><td hidden='hidden'>"+planTemplateList[i].positionCode+"</td></tr> ")
						    $("#edittbody").append(html);
				}
				var tablenum=$("#edittbody").find("tr");
				if(tablenum.length==0){
					$(".startplace").val("");
					$(".distination").val("");
				}
				if(tablenum.length==1){
					$("#edit_start_place").val($("#edittbody>tr:first-child>td:nth-child(2)").html());
					$("#edit_distination").val("");
				}else if(tablenum.length>1){
					$("#edit_start_place").val($("#edittbody>tr:first-child>td:nth-child(2)").html());
					$("#edit_distination").val($("#edittbody>tr:last-child>td:nth-child(2)").html());
				}
				$('#editWindow').modal({
			 		moveable:true,
			 		position: 30,
			 		show: true
			 	});
			}else{
				layer.open({
	 				  title: '消息提示',
	 				  offset: '200px',
	 				  content:"获取数据失败!"
	 				});
	 				$('body').scrollTop(100);
			}
	});
});
	//编辑确认位置与天数
	$("#editDetermine").on('click',function(){
		var tag=true;
		var positioncode= $("#add_position_code").val();
		 $.get($('body').attr('ctxPath')+'/planTemplate/findPositionByCode?t='+new Date().getTime()+"&positionCode="+positioncode,function(data){
				var btRegPosition=data.btRegPosition;
		if(btRegPosition==null&&data.regPositionCode!=''){
			tag=false;
			$("#add_position_code").val('');
			layer.msg("请选择一个位置,不要选择区域!");
			return;
		   }
		 if(tag){
				var PositionCode= $("#add_position_code").val();
				var PositionName=$("#addchoosePosition").val();
				var daynumber=$(".daynumber").val();
				var sign=new Number; 
				sign=$("#edittbody>tr:last-child>td:nth-child(1)").html();
				if (sign==undefined) {
					sign=1;
				}else{
					sign=parseInt(sign)+1;
				}
				var tablehtml1=("<tr><td>"+sign+"</td> <td>"+PositionName+"</td> <td>"+daynumber+"</td><td><a href='javascript:;' class='editdeleteJourlize'>删除</a></td><td hidden='hidden'>"+PositionCode+"</td></tr> ")
			    $("#edittbody").append(tablehtml1);
				var tablenum=$("#edittbody").find("tr");
				if(tablenum.length==0){
					$(".startplace").val("");
					$(".distination").val("");
				}
				if(tablenum.length==1){
					$(".startplace").val($(".Tbody>tr:first-child>td:nth-child(2)").html());
					$(".distination").val("");
				}else if(tablenum.length>1){
					$(".startplace").val($(".Tbody>tr:first-child>td:nth-child(2)").html());
					$(".distination").val($(".Tbody>tr:last-child>td:nth-child(2)").html());
				}
				$('#addTableModal').modal('hide');
				 }
				});
         });
	//删除编辑途经点
	 $('table>tbody').delegate('tr>td>.editdeleteJourlize', 'click', function(e) {
   	 $(this).parent().parent().remove();
   		var tablenum=$("#edittbody").find("tr");
			if(tablenum.length==0){
				$(".startplace").val("");
				$(".distination").val("");
			}
			if(tablenum.length==1){
				$("#edit_start_place").val($("#edittbody>tr:first-child>td:nth-child(2)").html());
				$("#edit_distination").val("");
			}else if(tablenum.length>1){
				$("#edit_start_place").val($("#edittbody>tr:first-child>td:nth-child(2)").html());
				$("#edit_distination").val($("#edittbody>tr:last-child>td:nth-child(2)").html());
			}
			var sign=0;
   		$("#edittbody").find("tr").each(function(){ 
   				$(this).children('td:eq(0)').text(sign+1);
   				 sign=sign+1;
   		});
        });
	//保存 编辑计划模板窗口的 信息
    $("#edit").on('click',function(){
   	var tag=true;
   	var plantemplateid=$("#editid").val();
   	var startplace= $("#edit_start_place").val();
   	var distination= $("#edit_distination").val();
   	var addremarks= $("#editremarks").val();
   	var plantemplatecode= $("#editplanTemplateCode").val();
   	var plantemplatename= $("#editplanTemplateName").val();
   	var transport= $("#edittransport").val();
   	var distance= $("#edit_distance").val();
   	if(plantemplatename==""){
   		tag=false;
   		layer.msg("请填写模版名称");
   		return;
   	}
   	if(addremarks==""){
   		tag=false;
   		layer.msg("请填写模版描述");
   		return;
   	}
   	if(transport==-1){
   		layer.msg("请选择运输方式");
   		tag=false;
   		return;
   	}
   	
   	if(tag){
   		    //table sign装箱
	    	 var signArr = new Array();
			 var signStr;
			  $("#edittbody").find("tr").each(function () {
				  signArr.push($(this).children('td:eq(0)').text().trim());
				  signStr=signArr.join(";");
			  });
			 //table position装箱
		  	 var positionArr = new Array();
			 var positionStr;
			  $("#edittbody").find("tr").each(function () {
				  positionArr.push($(this).children('td:eq(1)').text().trim());
				  positionStr=positionArr.join(";");
				  });
		    //table day装箱
	    	 var dayArr = new Array();
			 var dayStr;
			  $("#edittbody").find("tr").each(function () {
				  dayArr.push($(this).children('td:eq(2)').text().trim());
				  dayStr=dayArr.join(";");
			  });
			  //table positionCode装箱
	    	 var positionCodeArr = new Array();
			 var positionCodeStr;
			  $("#edittbody").find("tr").each(function () {
				  positionCodeArr.push($(this).children('td:eq(4)').text().trim());
				  positionCodeStr=positionCodeArr.join(";");
			  });	  
			  $.post($('body').attr('ctxPath') +'/planTemplate/editPlanTemplate?t='+new Date().getTime(),
			      {"plantemplateid":plantemplateid,"startplace":startplace,"distination":distination,"addremarks":addremarks,"plantemplatecode":plantemplatecode,"plantemplatename":plantemplatename,
				  "transport":transport, "distance":distance,"signStr":signStr,"positionStr":positionStr,"dayStr":dayStr,"positionCodeStr":positionCodeStr},function(data){
					  layer.open({
						  title: '消息提示',
						  offset: '200px',
						  content: data.msg
					  });
					  $('#editWindow').modal('hide');
					  $('body').scrollTop(100);
					  listui.refresh('mainUI');
			  });
      	}
});

//数据表格删除操作
$('.list-content-div table>tbody').delegate('tr>td>.delete', 'click', function() {
	var id = $(this).attr('data-id');
	//根据id获取位置信息
	layer.confirm('确定删除吗？', {
		  btn: ['确认','取消'] //按钮
		}, function(){
	$.get($('body').attr('ctxPath')+'/planTemplate/deletePlanTemplate/'+id+'?t='+new Date().getTime(),function(data){
			if(data.state){
				 listui.refresh('mainUI');
				layer.open({
	 				  title: '消息提示',
	 				  offset: '200px',
	 				  content: data.msg
	 				});
	 				$('body').scrollTop(100);
			}else{
				layer.open({
	 				  title: '消息提示',
	 				  offset: '200px',
	 				  content:"删除失败!"
	 				});
	 				$('body').scrollTop(100);
			}
		});
	})
	});
//数据表格操作按钮
function handleOpRow(tr, obj, container) {
	tr.append($('<td/>').addClass('op').append($('<a/>', {
		"href" : "javascript:;",
		"data-id" : obj.id
	}).addClass('edits').text('编辑')).append($('<span/>').text(' | ')).append($('<a/>', {
		"href" : "javascript:;",
		"data-id" : obj.id
	}).addClass('delete').text('删除')));
}

//按指定位置删除数组元素
Array.prototype.removeIndex = function (index) {
  if (index > - 1) {
    this.splice(index, 1);
  }
};
/*sign=$(".Tbody>tr:eq(0)>td:nth-child(1)").html();
//.Tbody表格第N+1个tr第N个td的值。*/
